package com.xmh.test.other.myThread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.google.common.util.concurrent.SimpleTimeLimiter;
import com.google.common.util.concurrent.TimeLimiter;
import com.xmh.test.other.myThread.service.UserInfoService;
import com.xmh.test.other.myThread.service.impl.UserInfoServiceImpl;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring_bean.xml")
public class ThreadOne {

	private final Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	private ThreadPoolTaskExecutor poolTaskExecutor;

	private final ExecutorService executor = Executors.newSingleThreadExecutor();

	@SuppressWarnings("rawtypes")
	@Test
	public void test1() {
		Callable<Object> task = () -> {
			while (true) {
				System.out.println(1);
			}
		};
		Future future = poolTaskExecutor.submit(task);
		try {
			future.get(1, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			logger.error(e);
		} catch (ExecutionException e) {
			logger.error(e);
		} catch (TimeoutException e) {
			logger.error(e);
		}
		System.out.println(22222);
	}

	@Test
	public void test2() {
		SimpleTimeLimiter timeLimiter = SimpleTimeLimiter.create(executor);

		Callable<Object> work = () -> {
			while (true) {
				System.out.println(1);
			}
		};
		try {
			callWithTimeout(work, 1, TimeUnit.SECONDS, false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public <T> T callWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit,
			boolean amInterruptible) throws Exception {
		Future<T> future = this.executor.submit(callable);
		try {
			return (T) future.get(timeoutDuration, timeoutUnit);
		} catch (InterruptedException var8) { // 抛出中断异常
			future.cancel(true);
			throw var8;
		}
	}

	public String getUserName() throws InterruptedException {
		System.out.println("123");
		Thread.sleep(500);
		System.out.println("456");
		return "tim";
	}

	@Test
	public void test3() {
		TimeLimiter timeLimiter = SimpleTimeLimiter.create(executor);

		UserInfoService userInfoService = new UserInfoServiceImpl();

		UserInfoService userInfoService1 = timeLimiter.newProxy(userInfoService, UserInfoService.class, 20,
				TimeUnit.MILLISECONDS);

		try {
			String userName = userInfoService1.getUserName();
			System.out.println("userName:" + userName);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void test4() throws InterruptedException {
//		Callable<String> task = new Callable<String>() {
//			@Override
//			public String call() throws Exception {
//				// 执行耗时代码
//				Thread.sleep(10000);
//				System.out.println("1");
//				return "success";
//			}
//		};
//		ExecutorService executorService = Executors.newSingleThreadExecutor();
//		Future<String> future = executorService.submit(() -> {
//			Thread.sleep(2000);
//			System.out.println("1");
//			return "success";
//		});
//		ExecutorService executorService = Executors.newSingleThreadExecutor();
		Future<String> future = poolTaskExecutor.submit(() -> {
			for (int i = 0; i < 2; i++) {
				Thread.sleep(1000);
				System.out.println(i);
			}
			return "sueecs";
		});
		try {
			// 设置超时时间
			String rst = future.get(1, TimeUnit.SECONDS);
			System.out.println(rst);
		} catch (TimeoutException e) {
			System.out.println("执行超时");
		} catch (Exception e) {
			System.out.println("获取数据异常," + e.getMessage());
		} finally {
//			executorService.shutdown();
			future.cancel(true);
		}
		Thread.sleep(100000L);
	}

}
